/**
 * 网络请求配置
 */
import axios from "axios";
import {message} from 'antd';


const service = axios.create({
    // axios中请求配置有baseURL选项，表示请求URL公共部分
    baseURL: process.env.REACT_APP_BASE_URL,
    //baseURL: 'http://192.168.3.111:8888',
    // 超时
    timeout: 10000
})


/**
 * http request 拦截器
 */
service.interceptors.request.use(
    (config) => {
        //  config.data = JSON.stringify(config.data);
        config.headers = {
            "Content-Type": "application/json",
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Headers": "Authorization,Origin, X-Requested-With, Content-Type, Accept",
            Authorization: "Basic eGlhb3NoaToxMjM0cXdlcg==",
        };

        return config;
    },
    (error) => {
        return Promise.reject(error);
    }
);

/**
 * http response 拦截器
 */
service.interceptors.response.use(
    (response: any) => {

        console.log(3, response);

        //console.log(1234,response);
        //  if(response.data.code !=='200'){
        //     msag(response.data)
        //  }
        //console.log(response);
        //  if (response.data.errCode === 2) {
        //    console.log("过期");
        //  }
        return response;
    },
    (error: any) => {
        errorMsg(error.response.data)
    }
);


export default service


export function login(params: any) {

    let url: string = "/oauth/token"
    return new Promise((resolve, reject) => {
        service.post(url, null, {
            params: params,
        }).then((response) => {
            resolve(response);
        })
            .catch((error) => {
                reject(error);
            });
    });

}


/**
 * 封装get方法
 * @param url  请求url
 * @param params  请求参数
 * @returns {Promise}
 */
export function get(url: string, params = {}) {
    return new Promise((resolve, reject) => {
        service.get(url, {
            params: params,
        }).then((response) => {
            resolve(response.data);
        })
            .catch((error) => {
                reject(error);
            });
    });
}

/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function post(url: string, data: object) {
    return new Promise((resolve, reject) => {
        service.post(url, data).then(
            (response) => {
                //关闭进度条
                resolve(response.data);
            },
            (err) => {
                reject(err);
            }
        );
    });
}

/**
 * 封装patch请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function patch(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        service.patch(url, data).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                errorMsg(err);
                reject(err);
            }
        );
    });
}

/**
 * 封装put请求
 * @param url
 * @param data
 * @returns {Promise}
 */

export function put(url: string, data = {}) {
    return new Promise((resolve, reject) => {
        service.put(url, data).then(
            (response) => {
                resolve(response.data);
            },
            (err) => {
                errorMsg(err);
                reject(err);
            }
        );
    });
}


function error(msg: string) {
    message.error(msg);
};

/**
 * 失败得提示any
 * @param {} data
 */
function errorMsg(data: any) {
    if (data?.code) {
        switch (data.code) {
            case 400:
                error(data.msg);
                break;
            case 401:
                error("未授权，请登录");
                break;

            case 403:
                error("拒绝访问");
                break;

            case 404:
                error("请求地址出错");
                break;

            case 408:
                error("请求超时");
                break;

            case 500:
                error(data.msg)
                break;

            case 501:
                error("服务未实现");
                break;

            case 502:
                error("网关错误");
                break;

            case 503:
                error("服务不可用");
                break;

            case 504:
                error("网关超时");
                break;

            case 505:
                error("HTTP版本不受支持");
                break;
            default:
        }
    }


}



